/*
 * Helstius--Basic chemistry application
 * This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 * Copyright 2012 Jaked122
 */
package org.helstius.Gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.Toolkit;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;

import org.helstius.chem.Parser;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.HashMap;

public class EquationBalancingGui extends JFrame {

	private JPanel contentPane;
	private EquationValidation e;
	private EquationValidation reagent2;
	private JSplitPane splitPane;
	private JTabbedPane tabbedPane_1;
	private EquationValidation Product1;
	private EquationValidation Product2;
	private JTabbedPane tabbedPane_2;
	private JTextArea ReportPane;
	private JPopupMenu popupMenu;
	private JMenuItem mntmFindMultiples;
	private EquationValidation reagent3;
	private EquationValidation product3;

	/**
	 * Create the frame.
	 */
	public EquationBalancingGui() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("Chemical Equations");
		setIconImage(Toolkit.getDefaultToolkit().getImage(
				EquationBalancingGui.class
						.getResource("/org/helstius/Gui/Icons/equation.png")));
		setBounds(100, 100, 450, 348);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.SOUTH);

		tabbedPane_2 = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane_2.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				if (tabbedPane_2.getSelectedIndex() == 1) {
					StringBuilder q = new StringBuilder();
					q.append("Reactants:");
					q.append("\n");
					q.append(Parser.GetRepresentation(Parser.AddMaps(
							EquationBalancingGui.this.e.calculatecount(),
							reagent2.calculatecount(),
							reagent3.calculatecount())));
					q.append("\n");
					q.append("Products" + "\n");
					q.append(Parser.GetRepresentation(Parser.AddMaps(
							Product1.calculatecount(),
							Product2.calculatecount(),
							product3.calculatecount())));
					q.append("Overall balance" + "\n");
					q.append(Parser.getRepresentation(Parser.comparecounts(
							Parser.AddMaps(EquationBalancingGui.this.e
									.calculatecount(), reagent2
									.calculatecount(), reagent3
									.calculatecount()), Parser.AddMaps(
									Product1.calculatecount(),
									Product2.calculatecount(),
									product3.calculatecount()))));
					ReportPane.setText(q.toString());
				}
			}
		});
		contentPane.add(tabbedPane_2, BorderLayout.CENTER);

		splitPane = new JSplitPane();
		tabbedPane_2.addTab("Equation", null, splitPane, null);

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT);
		splitPane.setLeftComponent(tabbedPane);
		e = new EquationValidation();
		tabbedPane.addTab("Reagent 1", null, e, null);

		reagent2 = new EquationValidation();
		tabbedPane.addTab("Reagent 2", null, reagent2, null);

		reagent3 = new EquationValidation();
		tabbedPane.addTab("New tab", null, reagent3, null);

		tabbedPane_1 = new JTabbedPane(JTabbedPane.LEFT);
		splitPane.setRightComponent(tabbedPane_1);

		Product1 = new EquationValidation();
		tabbedPane_1.addTab("Product 1", null, Product1, null);
		tabbedPane_1.setEnabledAt(0, true);

		Product2 = new EquationValidation();
		tabbedPane_1.addTab("Product2", null, Product2, null);

		product3 = new EquationValidation();
		tabbedPane_1.addTab("New tab", null, product3, null);

		ReportPane = new JTextArea();
		ReportPane.setLineWrap(true);
		tabbedPane_2.addTab("Report", null, ReportPane, null);

		popupMenu = new JPopupMenu();
		addPopup(ReportPane, popupMenu);

		mntmFindMultiples = new JMenuItem("Find Multiples");
		mntmFindMultiples.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent f) {
				// okay, so this is, without exaggeration, the worst way I could
				// have done this.
				for (int i = 1; i < 10; i++) {
					e.setCoefficient(i);
					HashMap<String, Integer> ce = e.calculatecount();
					for (int b = 1; b < 10; b++) {
						reagent2.setCoefficient(b);
						HashMap<String, Integer> c2 = reagent2.calculatecount();
						for (int c = 1; c < 10; c++) {
							reagent3.setCoefficient(c);
							HashMap<String, Integer> cr3 = reagent3
									.calculatecount();
							HashMap<String, Integer> cs = Parser.AddMaps(ce,
									c2, cr3);
							for (int d = 1; d < 10; d++) {
								Product2.setCoefficient(d);
								HashMap<String, Integer> pr2 = Product2
										.calculatecount();
								for (int u = 1; u < 10; u++) {
									Product1.setCoefficient(u);
									HashMap<String, Integer> pr1 = Product1
											.calculatecount();
									for (int k = 1; k < 10; k++) {
										product3.setCoefficient(k);
										HashMap<String, Integer> pr3 = product3
												.calculatecount();

										if (Parser.countsequal(Parser.comparecounts(
												cs,
												Parser.AddMaps(pr1, pr2, pr3)))) {
											return;
										}
									}
								}
							}
						}
					}

				}

			}
		});
		popupMenu.add(mntmFindMultiples);
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
